﻿#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
using namespace std;

//1.参数类型不同构成重载
int Add(int a, int b)
{
	return a + b;
}

double Add(double a, double b)
{
	return a + b;
}

//2.参数个数不同构成重载
void func()
{
	cout << "func()" << endl;
}

void func(int a)
{
	cout << "func(int a)" << endl;
}

//3.参数顺序不同构成重载
double Add(int a, double b)
{
	return a + b;
}

double Add(double a, int b)
{
	return a + b;
}

int func1()
{
	return 1;
}
//double func1()
//{
//	return 3.2;
//}

void func3()
{
	cout << "func3()" << endl;
}

void func3(int a=10)//给上缺省值
{
	cout << "func3(int a=10)" << endl;
}

int main1()
{
	/*cout << Add(3, 2) << endl;
	cout << Add(3.2, 2.3) << endl;*/
	/*func();
	func(0);*/

	//cout << Add(3, 2.2) << endl;
	//cout << Add(3.2, 2) << endl;

	//无歧义的调用
	func3(30);

	//有歧义的调用
	//func3();
	return 0;
}

int main2()
{
	//int a=0;
	//// 引⽤：b和c是a的别名
	//int& b=a;
	//int& c = a;
	//// 也可以给别名b取别名，d相当于还是a的别名
	//int& d = b;
	//++d;
	//// 这⾥取地址我们看到是⼀样的
	//cout << &a << endl;
	//cout << &b << endl;
	//cout << &c << endl;
	//cout << &d << endl;

	int a = 10;
	int b = 20;
	//给a取一个别名叫c
	int& c = a;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	cout << "c=" << c << endl<<"\n";
	//这里不是给b取一个别名叫c，
	//而是给c赋值为20（也就相当于原来的a变为了20）
	c = b;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	cout << "c=" << c<< endl;

	return 0;
}

//引用传参
void swap(int& a, int& b)
{
	int tmp = a;
	a = b;
	b = tmp;
}

int main3()
{
	int a = 10;
	int b = 20;
	cout << "交换之前" << endl;
	cout << "a=" <<a<<" b= "<<b << endl;
	swap(a, b);
	cout << "交换之后" << endl;
	cout << "a=" << a << " b=" << b << endl;
	return 0;
}



int main4()
{
	//常变量必须用常量引用
	const int a = 10;
	const int& b = a;//权限平移
	//int& b = a;//权限变大，编译报错

	//普通变量既可以用普通引用
	int c = 10;
	int& d = c;//权限平移

	//也可以用const引用
	const int& e = c;//权限缩小

	return 0;
}

int main()
{
	int a = 3;
	//int& ra = a * 3;//a*3的结果会储存在一个临时对象中而C++规定临时对象具有常性
	const int& raa = a * 3;

	double c = 5.3;
	//int& rc = c;//int到double发生类型转换也会产生临时对象储存中间值
	const int& rcc = c;

	return 0;
}